/*
 * @Author: GY19531 418941369@qq.com
 * @Date: 2022-09-05 14:40:10
 * @LastEditors: GY19531 418941369@qq.com
 * @LastEditTime: 2022-09-06 02:05:39
 * @FilePath: \one-web\src\pages\RemindManage\common\useReducer.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import React, { Dispatch, useReducer } from 'react';

export enum IActionEnum {
    LEFTAUTO_PARENT = 'leftAutoExpandParent',
    LEFTSELECT_KEYS = 'leftSelectKeys',
    LEFTEXPANDED_KEYS = 'leftExpandedKeys',
    CENTREUSER_ROWS = 'centreUserRows',
    LEFTSELECT_KEYS_COPY = 'leftSelectKeysCopy',
    LEFTEXPANDED_KEYS_COPY = 'leftExpandedKeysCopy',
    CENTREUSER_ROWS_COPY = 'centreUserRowsCopy',
    LEFTDEPT_USER = 'deptUserList',
    LEFTDEPT_USER_COPY = 'deptUserListCopy',
    CENTRE_CHECKBOX_KEYS = 'joinCheckBoxKeys',
    CENTRE_CHECKBOX_KEYS_COPY = 'copyCheckBoxKeys',
    CENTRE_CHECKBOX_ROWS = 'joinCheckBoxRows',
    CENTRE_CHECKBOX_ROWS_COPY = 'copyCheckBoxRows',
    SAVE_USER_LIST = 'saveUserList',
    SAVE_USER_LIST_COPY = 'saveUserListCopy',
    LEFTDEPT_USER_LOADING = 'userLoading',
    REMOVE_STATE = 'remove-state',
}

/** 定义新增和编辑页面操作匹配 */
export interface IActionType {
    type: IActionEnum;
    payload?: any;
}

export const useUpdateReducer = () => {
    const initState = {
        leftAutoExpandParent: false,
        leftSelectKeys: [],
        leftExpandedKeys: [],
        centreUserRows: [],
        leftSelectKeysCopy: [],
        leftExpandedKeysCopy: [],
        centreUserRowsCopy: [],
        deptUserList: {},
        deptUserListCopy: {},
        joinCheckBoxKeys: [],
        copyCheckBoxKeys: [],
        joinCheckBoxRows: [],
        copyCheckBoxRows: [],
        saveUserList: [],
        saveUserListCopy: [],
        userLoading: false,
    }

    const upDateReducer = (state = initState, action: IActionType) => {

        switch (action?.type) {
            case IActionEnum.LEFTDEPT_USER_LOADING:
                return {
                    ...state,
                    userLoading: action?.payload,
                };

            /**
             * 保存选中的用户 数据 抄送人
             */
            case IActionEnum.SAVE_USER_LIST_COPY:
                return {
                    ...state,
                    saveUserListCopy: action.payload,
                };

            /**
             * 保存选中的用户 数据 参与人
             */
            case IActionEnum.SAVE_USER_LIST:
                return {
                    ...state,
                    saveUserList: action.payload,
                };

            /**
             * 参与人选中用户
             */
            case IActionEnum.CENTRE_CHECKBOX_ROWS:
                return {
                    ...state,
                    joinCheckBoxRows: action?.payload,
                };

            /**
             * 抄送人选中用户
             */
            case IActionEnum.CENTRE_CHECKBOX_ROWS_COPY:
                return {
                    ...state,
                    copyCheckBoxRows: action?.payload,
                };

            /**
             * 参与人选中用户
             */
            case IActionEnum.CENTRE_CHECKBOX_KEYS:
                return {
                    ...state,
                    joinCheckBoxKeys: action?.payload,
                };

            /**
             * 抄送人选中用户
             */
            case IActionEnum.CENTRE_CHECKBOX_KEYS_COPY:
                return {
                    ...state,
                    copyCheckBoxKeys: action?.payload,
                };

            /**
             * 参与人左侧选中用户
             */
            case IActionEnum.LEFTDEPT_USER:
                return {
                    ...state,
                    deptUserList: action?.payload,
                };
            /**
             * 抄送人人左侧选中用户
             */
            case IActionEnum.LEFTDEPT_USER_COPY:
                return {
                    ...state,
                    deptUserListCopy: action?.payload,
                };

            case IActionEnum.CENTREUSER_ROWS:
                return {
                    ...state,
                    centreUserRows: action?.payload,
                };

            case IActionEnum.LEFTAUTO_PARENT:
                return {
                    ...state,
                    leftAutoExpandParent: action?.payload,
                };

            /**
             * 左侧树选中的数据的key
             */
            case IActionEnum.LEFTSELECT_KEYS:
                return {
                    ...state,
                    leftSelectKeys: action?.payload,
                };

            /**
             * 左侧树 展开节点 key
             */
            case IActionEnum.LEFTEXPANDED_KEYS:
                return {
                    ...state,
                    leftExpandedKeys: action?.payload,
                };

            /**抄送人 */
            case IActionEnum.CENTREUSER_ROWS_COPY:
                return {
                    ...state,
                    centreUserRowsCopy: action?.payload,
                };

            /**
             * 左侧树选中的数据的key
             */
            case IActionEnum.LEFTSELECT_KEYS_COPY:
                return {
                    ...state,
                    leftSelectKeysCopy: action?.payload,
                };

            /**
             * 左侧树 展开节点 key
             */
            case IActionEnum.LEFTEXPANDED_KEYS_COPY:
                return {
                    ...state,
                    leftExpandedKeysCopy: action?.payload,
                };

            case IActionEnum.REMOVE_STATE:
                return {
                    ...state,
                    ...initState,
                };
            default:
                return {
                    ...state,
                };
        }

    }

    const [state, updateDispath] = useReducer(upDateReducer, initState)

    return {
        ...state,
        updateDispath
    }
}